package com.xiaoran.actiiveNIO.queue_DelaySchedule;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class JmsConsumer {
    public static final String ACTIVEMQ_URL="failover:(tcp://192.168.171.128:61616,tcp://192.168.171.132:61616,tcp://192.168.171.131:61616)?randomize=false";
    public static final String QUEUE_NAMQ="queue-Delay";
    public static void main(String args[]) throws JMSException, IOException {
        System.out.println("我是一号消费者");
        //1创建连接工场，，按照给定的url地址，采用默认的用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2通过连接工厂获得连接，获得connection并启动
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        //3.创建会话session
        Session session= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地
        Queue queue=session.createQueue(QUEUE_NAMQ);
        //5创建消费者,指定目的地
        MessageConsumer messageConsumer=session.createConsumer(queue);
        //通过监听器的方式来处理消息
        messageConsumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                if(message!=null && message instanceof TextMessage){
                    TextMessage textMessage=(TextMessage) message;
                    try {
                        System.out.println("cluster **********"+textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        System.in.read();
        messageConsumer.close();
        session.close();
        connection.close();
}
}
